package usbcali.edu.co.pqrs.presentation.backingBeans;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;

import usbcali.edu.co.pqrs.exceptions.ZMessManager;
import usbcali.edu.co.pqrs.modelo.MotSolXTpSol;
import usbcali.edu.co.pqrs.modelo.MotivoSolicitud;
import usbcali.edu.co.pqrs.modelo.TipoSolicitudPqr;
import usbcali.edu.co.pqrs.modelo.dto.MotSolXTpSolDTO;
import usbcali.edu.co.pqrs.modelo.dto.MotivoSolicitudDTO;
import usbcali.edu.co.pqrs.modelo.dto.TipoSolicitudPqrDTO;
import usbcali.edu.co.pqrs.presentation.businessDelegate.IBusinessDelegatorView;
import usbcali.edu.co.pqrs.utilities.FacesUtils;

/**
 * @author Zathura Code Generator http://code.google.com/p/zathura
 * 
 */
@ManagedBean
@ViewScoped
public class MotSolXTpSolView implements Serializable {
	private static final long serialVersionUID = 1L;

	private Long motivoSolicitud;
	private Long tipoSolicitudPqr;
	private SelectOneMenu txtTipoSolicitud;
	private SelectOneMenu txtMotivoSolicitud;
	private Map<String, String> losItemsTipoSolicitud = new HashMap<String, String>();
	private Map<String, String> losItemsMotivoSolicitud = new HashMap<String, String>();
	private CommandButton btnBack;
	List<MotivoSolicitudDTO> data2;
	List<TipoSolicitudPqrDTO> data3;

	private InputText txtIdMotSol_MotivoSolicitud;
	private InputText txtIdTpSolPqr_TipoSolicitudPqr;
	private InputText txtIdMotSolXTpSol;
	private CommandButton btnSave;
	private CommandButton btnModify;
	private CommandButton btnDelete;
	private CommandButton btnClear;
	private List<MotSolXTpSolDTO> data;
	private MotSolXTpSolDTO selectedMotSolXTpSol;
	private MotSolXTpSol entity;
	private boolean showDialog;
	@ManagedProperty(value = "#{BusinessDelegatorView}")
	private IBusinessDelegatorView businessDelegatorView;

	private SelectItem[] manufacturerOptions;
	String manufacturers[] = { "A", "R" };

	public MotSolXTpSolView() {
		super();
		setManufacturerOptions(createFilterOptions(manufacturers));
	}

	private SelectItem[] createFilterOptions(String[] data) {
		SelectItem[] options = new SelectItem[data.length + 1];

		options[0] = new SelectItem("", "Seleccionar");
		for (int i = 0; i < data.length; i++) {
			options[i + 1] = new SelectItem(data[i], data[i]);
		}

		return options;
	}

	public void rowEventListener(RowEditEvent e) {
		try {
			MotSolXTpSolDTO motSolXTpSolDTO = (MotSolXTpSolDTO) e.getObject();

			if (txtIdMotSol_MotivoSolicitud == null) {
				txtIdMotSol_MotivoSolicitud = new InputText();
			}

			txtIdMotSol_MotivoSolicitud.setValue(motSolXTpSolDTO
					.getIdMotSol_MotivoSolicitud());

			if (txtIdTpSolPqr_TipoSolicitudPqr == null) {
				txtIdTpSolPqr_TipoSolicitudPqr = new InputText();
			}

			txtIdTpSolPqr_TipoSolicitudPqr.setValue(motSolXTpSolDTO
					.getIdTpSolPqr_TipoSolicitudPqr());

			if (txtMotivoSolicitud == null) {
				txtMotivoSolicitud = new SelectOneMenu();
			}
			txtMotivoSolicitud.setValue(motSolXTpSolDTO
					.getIdMotSol_MotivoSolicitud());

			if (txtTipoSolicitud == null) {
				txtTipoSolicitud = new SelectOneMenu();
			}

			txtTipoSolicitud.setValue(motSolXTpSolDTO
					.getIdTpSolPqr_TipoSolicitudPqr());

			if (txtIdMotSolXTpSol == null) {
				txtIdMotSolXTpSol = new InputText();
			}

			txtIdMotSolXTpSol.setValue(motSolXTpSolDTO.getIdMotSolXTpSol());

			Long idMotSolXTpSol = FacesUtils.checkLong(txtIdMotSolXTpSol);
			entity = businessDelegatorView.getMotSolXTpSol(idMotSolXTpSol);

			action_modify();
		} catch (Exception ex) {
		}
	}

	public String action_new() {
		action_clear();
		selectedMotSolXTpSol = null;
		setShowDialog(true);

		return "";
	}

	public String action_clear() {
		entity = null;
		selectedMotSolXTpSol = null;

		if (txtIdMotSol_MotivoSolicitud != null) {
			txtIdMotSol_MotivoSolicitud.setValue(null);
			txtIdMotSol_MotivoSolicitud.setDisabled(true);
		}

		if (txtIdTpSolPqr_TipoSolicitudPqr != null) {
			txtIdTpSolPqr_TipoSolicitudPqr.setValue(null);
			txtIdTpSolPqr_TipoSolicitudPqr.setDisabled(true);
		}

		if (txtIdMotSolXTpSol != null) {
			txtIdMotSolXTpSol.setValue(null);
			txtIdMotSolXTpSol.setDisabled(false);
		}

		if (btnSave != null) {
			btnSave.setDisabled(true);
		}

		if (btnDelete != null) {
			btnDelete.setDisabled(true);
		}

		return "";
	}

	public String action_back() {

		return "initialMenu.xhtml?faces-redirect=true";
	}

	public void listener_txtId() {
		try {
			Long idMotSolXTpSol = FacesUtils.checkLong(txtIdMotSolXTpSol);
			entity = (idMotSolXTpSol != null) ? businessDelegatorView
					.getMotSolXTpSol(idMotSolXTpSol) : null;
		} catch (Exception e) {
			entity = null;
		}

		if (entity == null) {
			txtIdMotSol_MotivoSolicitud.setDisabled(false);
			txtIdTpSolPqr_TipoSolicitudPqr.setDisabled(false);
			txtIdMotSolXTpSol.setDisabled(false);
			btnSave.setDisabled(false);
		} else {
			txtIdMotSol_MotivoSolicitud.setValue(entity.getMotivoSolicitud()
					.getIdMotSol());
			txtIdMotSol_MotivoSolicitud.setDisabled(false);
			txtIdTpSolPqr_TipoSolicitudPqr.setValue(entity
					.getTipoSolicitudPqr().getIdTpSolPqr());
			txtIdTpSolPqr_TipoSolicitudPqr.setDisabled(false);
			txtIdMotSolXTpSol.setValue(entity.getIdMotSolXTpSol());
			txtIdMotSolXTpSol.setDisabled(true);
			btnSave.setDisabled(false);

			if (btnDelete != null) {
				btnDelete.setDisabled(false);
			}
		}
	}

	public String action_edit(ActionEvent evt) {
		selectedMotSolXTpSol = (MotSolXTpSolDTO) (evt.getComponent()
				.getAttributes().get("selectedMotSolXTpSol"));
		txtIdMotSol_MotivoSolicitud.setValue(selectedMotSolXTpSol
				.getIdMotSol_MotivoSolicitud());
		txtIdMotSol_MotivoSolicitud.setDisabled(false);
		txtIdTpSolPqr_TipoSolicitudPqr.setValue(selectedMotSolXTpSol
				.getIdTpSolPqr_TipoSolicitudPqr());
		txtIdTpSolPqr_TipoSolicitudPqr.setDisabled(false);
		txtIdMotSolXTpSol.setValue(selectedMotSolXTpSol.getIdMotSolXTpSol());
		txtIdMotSolXTpSol.setDisabled(true);
		btnSave.setDisabled(false);
		setShowDialog(true);

		return "";
	}

	public String action_save() {
		try {
			if ((selectedMotSolXTpSol == null) && (entity == null)) {
				action_create();
			} else {
				action_modify();
			}

			data = null;
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_create() {
		try {
			entity = new MotSolXTpSol();

			MotivoSolicitud motivoSolicitudItem = businessDelegatorView
					.getMotivoSolicitud(motivoSolicitud);
			TipoSolicitudPqr tipoSolicitudPqrItem = businessDelegatorView
					.getTipoSolicitudPqr(tipoSolicitudPqr);

			entity.setMotivoSolicitud(motivoSolicitudItem);
			entity.setTipoSolicitudPqr(tipoSolicitudPqrItem);

			businessDelegatorView.saveMotSolXTpSol(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
			data = businessDelegatorView.getDataMotSolXTpSol();
			RequestContext.getCurrentInstance().update("form:listaTDo");
		} catch (Exception e) {
			entity = null;
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_modify() {
		try {
			if (entity == null) {
				Long idMotSolXTpSol = new Long(
						selectedMotSolXTpSol.getIdMotSolXTpSol());
				entity = businessDelegatorView.getMotSolXTpSol(idMotSolXTpSol);
			}

			entity.setMotivoSolicitud((FacesUtils
					.checkLong(txtIdMotSol_MotivoSolicitud) != null) ? businessDelegatorView
					.getMotivoSolicitud(FacesUtils
							.checkLong(txtIdMotSol_MotivoSolicitud)) : null);
			entity.setTipoSolicitudPqr((FacesUtils
					.checkLong(txtIdTpSolPqr_TipoSolicitudPqr) != null) ? businessDelegatorView
					.getTipoSolicitudPqr(FacesUtils
							.checkLong(txtIdTpSolPqr_TipoSolicitudPqr)) : null);
			businessDelegatorView.updateMotSolXTpSol(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			data = null;
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete_datatable(ActionEvent evt) {
		try {
			selectedMotSolXTpSol = (MotSolXTpSolDTO) (evt.getComponent()
					.getAttributes().get("selectedMotSolXTpSol"));

			Long idMotSolXTpSol = new Long(
					selectedMotSolXTpSol.getIdMotSolXTpSol());
			entity = businessDelegatorView.getMotSolXTpSol(idMotSolXTpSol);
			action_delete();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete_master() {
		try {
			Long idMotSolXTpSol = FacesUtils.checkLong(txtIdMotSolXTpSol);
			entity = businessDelegatorView.getMotSolXTpSol(idMotSolXTpSol);
			action_delete();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public void action_delete() throws Exception {
		try {
			businessDelegatorView.deleteMotSolXTpSol(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
			data = null;
		} catch (Exception e) {
			throw e;
		}
	}

	public String action_closeDialog() {
		setShowDialog(false);
		action_clear();

		return "";
	}

	public String actionDeleteDataTableEditable(ActionEvent evt) {
		try {
			selectedMotSolXTpSol = (MotSolXTpSolDTO) (evt.getComponent()
					.getAttributes().get("selectedMotSolXTpSol"));

			Long idMotSolXTpSol = new Long(
					selectedMotSolXTpSol.getIdMotSolXTpSol());
			entity = businessDelegatorView.getMotSolXTpSol(idMotSolXTpSol);
			businessDelegatorView.deleteMotSolXTpSol(entity);
			data.remove(selectedMotSolXTpSol);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_modifyWitDTO(Long idMotSolXTpSol,
			Long idMotSol_MotivoSolicitud, Long idTpSolPqr_TipoSolicitudPqr)
			throws Exception {
		try {
			businessDelegatorView.updateMotSolXTpSol(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			// renderManager.getOnDemandRenderer("MotSolXTpSolView").requestRender();
			FacesUtils.addErrorMessage(e.getMessage());
			throw e;
		}

		return "";
	}

	public InputText getTxtIdMotSol_MotivoSolicitud() {
		return txtIdMotSol_MotivoSolicitud;
	}

	public void setTxtIdMotSol_MotivoSolicitud(
			InputText txtIdMotSol_MotivoSolicitud) {
		this.txtIdMotSol_MotivoSolicitud = txtIdMotSol_MotivoSolicitud;
	}

	public InputText getTxtIdTpSolPqr_TipoSolicitudPqr() {
		return txtIdTpSolPqr_TipoSolicitudPqr;
	}

	public void setTxtIdTpSolPqr_TipoSolicitudPqr(
			InputText txtIdTpSolPqr_TipoSolicitudPqr) {
		this.txtIdTpSolPqr_TipoSolicitudPqr = txtIdTpSolPqr_TipoSolicitudPqr;
	}

	public InputText getTxtIdMotSolXTpSol() {
		return txtIdMotSolXTpSol;
	}

	public void setTxtIdMotSolXTpSol(InputText txtIdMotSolXTpSol) {
		this.txtIdMotSolXTpSol = txtIdMotSolXTpSol;
	}

	public List<MotSolXTpSolDTO> getData() {
		try {
			if (data == null) {
				data = businessDelegatorView.getDataMotSolXTpSol();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return data;
	}

	public void setData(List<MotSolXTpSolDTO> motSolXTpSolDTO) {
		this.data = motSolXTpSolDTO;
	}

	public MotSolXTpSolDTO getSelectedMotSolXTpSol() {
		return selectedMotSolXTpSol;
	}

	public void setSelectedMotSolXTpSol(MotSolXTpSolDTO motSolXTpSol) {
		this.selectedMotSolXTpSol = motSolXTpSol;
	}

	public CommandButton getBtnSave() {
		return btnSave;
	}

	public void setBtnSave(CommandButton btnSave) {
		this.btnSave = btnSave;
	}

	public CommandButton getBtnModify() {
		return btnModify;
	}

	public void setBtnModify(CommandButton btnModify) {
		this.btnModify = btnModify;
	}

	public CommandButton getBtnDelete() {
		return btnDelete;
	}

	public void setBtnDelete(CommandButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	public CommandButton getBtnClear() {
		return btnClear;
	}

	public void setBtnClear(CommandButton btnClear) {
		this.btnClear = btnClear;
	}

	public TimeZone getTimeZone() {
		return java.util.TimeZone.getDefault();
	}

	public IBusinessDelegatorView getBusinessDelegatorView() {
		return businessDelegatorView;
	}

	public void setBusinessDelegatorView(
			IBusinessDelegatorView businessDelegatorView) {
		this.businessDelegatorView = businessDelegatorView;
	}

	public boolean isShowDialog() {
		return showDialog;
	}

	public void setShowDialog(boolean showDialog) {
		this.showDialog = showDialog;
	}

	public void onEdit(org.primefaces.event.RowEditEvent event) {

		try {

			MotSolXTpSolDTO m = (MotSolXTpSolDTO) event.getObject();

			entity = null;
			entity = businessDelegatorView.getMotSolXTpSol((m
					.getIdMotSolXTpSol()));

			motivoSolicitud = m.getIdMotSol_MotivoSolicitud();
			tipoSolicitudPqr = m.getIdTpSolPqr_TipoSolicitudPqr();

			MotivoSolicitud motivoSolicitudItem = businessDelegatorView
					.getMotivoSolicitud(motivoSolicitud);

			TipoSolicitudPqr tipoSolicitudPqrItem = businessDelegatorView
					.getTipoSolicitudPqr(tipoSolicitudPqr);

			entity.setTipoSolicitudPqr(tipoSolicitudPqrItem);
			entity.setMotivoSolicitud(motivoSolicitudItem);

			businessDelegatorView.updateMotSolXTpSol(entity);

			/*
			 * try { List<TipoSolicitudPqrDTO> data = businessDelegatorView
			 * .getDataTipoSolicitudPqr();
			 * 
			 * for (int i = 0; i < data.size(); i++) {
			 * losItemsTipoSolicitud.put(data.get(i).getDescTpSol(), data
			 * .get(i).getIdTpSolPqr() + "");
			 * 
			 * } } catch (Exception e) { e.printStackTrace(); }
			 * 
			 * try { List<MotivoReclamacionDTO> data2 = businessDelegatorView
			 * .getDataMotivoReclamacion();
			 * 
			 * for (int i = 0; i < data2.size(); i++) {
			 * losItemsMotivoReclamacion.put(data2.get(i)
			 * .getDescripcionMotRecl(), data2.get(i) .getIdMotRecl() + "");
			 * 
			 * } } catch (Exception e) { e.printStackTrace(); }
			 * 
			 * businessDelegatorView.updateMotReclXTpSol(entity);
			 */

			data = businessDelegatorView.getDataMotSolXTpSol();

			RequestContext.getCurrentInstance().update("form:listaTDo");

			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void onCancel(org.primefaces.event.RowEditEvent event) {
		FacesMessage msg = new FacesMessage("TipoEstado Cancelled",
				((MotSolXTpSolDTO) event.getObject()).getIdMotSolXTpSol() + "");

		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public Long getMotivoSolicitud() {
		return motivoSolicitud;
	}

	public void setMotivoSolicitud(Long motivoSolicitud) {
		this.motivoSolicitud = motivoSolicitud;
	}

	public Long getTipoSolicitudPqr() {
		return tipoSolicitudPqr;
	}

	public void setTipoSolicitudPqr(Long tipoSolicitudPqr) {
		this.tipoSolicitudPqr = tipoSolicitudPqr;
	}

	public SelectOneMenu getTxtTipoSolicitud() {
		return txtTipoSolicitud;
	}

	public void setTxtTipoSolicitud(SelectOneMenu txtTipoSolicitud) {
		this.txtTipoSolicitud = txtTipoSolicitud;
	}

	public SelectOneMenu getTxtMotivoSolicitud() {
		return txtMotivoSolicitud;
	}

	public void setTxtMotivoSolicitud(SelectOneMenu txtMotivoSolicitud) {
		this.txtMotivoSolicitud = txtMotivoSolicitud;
	}

	public Map<String, String> getLosItemsTipoSolicitud() {
		try {
			if (data3 == null) {
				getData3();
				for (int i = 0; i < data3.size(); i++) {
					losItemsTipoSolicitud.put(data3.get(i).getDescTpSol(),
							data3.get(i).getIdTpSolPqr() + "");

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return losItemsTipoSolicitud;
	}

	public void setLosItemsTipoSolicitud(
			Map<String, String> losItemsTipoSolicitud) {
		this.losItemsTipoSolicitud = losItemsTipoSolicitud;
	}

	public Map<String, String> getLosItemsMotivoSolicitud() {
		try {
			if (data2 == null) {
				getData2();
				for (int i = 0; i < data2.size(); i++) {
					losItemsMotivoSolicitud.put(data2.get(i)
							.getDescripcionMotSol(), data2.get(i).getIdMotSol()
							+ "");

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return losItemsMotivoSolicitud;
	}

	public void setLosItemsMotivoSolicitud(
			Map<String, String> losItemsMotivoSolicitud) {
		this.losItemsMotivoSolicitud = losItemsMotivoSolicitud;
	}

	public CommandButton getBtnBack() {
		return btnBack;
	}

	public void setBtnBack(CommandButton btnBack) {
		this.btnBack = btnBack;
	}

	public List<MotivoSolicitudDTO> getData2() {
		try {
			if (data2 == null) {
				data2 = businessDelegatorView.getDataMotivoSolicitud();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return data2;
	}

	public void setData2(List<MotivoSolicitudDTO> data2) {
		this.data2 = data2;
	}

	public List<TipoSolicitudPqrDTO> getData3() {
		try {
			if (data3 == null) {
				data3 = businessDelegatorView.getDataTipoSolicitudPqr();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return data3;
	}

	public void setData3(List<TipoSolicitudPqrDTO> data3) {
		this.data3 = data3;
	}

	public MotSolXTpSol getEntity() {
		return entity;
	}

	public void setEntity(MotSolXTpSol entity) {
		this.entity = entity;
	}

	public SelectItem[] getManufacturerOptions() {
		return manufacturerOptions;
	}

	public void setManufacturerOptions(SelectItem[] manufacturerOptions) {
		this.manufacturerOptions = manufacturerOptions;
	}

	public String[] getManufacturers() {
		return manufacturers;
	}

	public void setManufacturers(String[] manufacturers) {
		this.manufacturers = manufacturers;
	}

}
